# 安装numpy: pip install numpy

import numpy as np

# 1、创建多维数组
array = np.array([[1, 2, 3], [4, 5, 6]])
print(array)
print(type(array))
# 矩阵转置
print(array.T)

arr2 = np.array(["1", "2", "4"])
print(arr2)

# 修改类型
arr2 = arr2.astype("float_")

print(arr2)

print("=" * 100)

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 切片
print(arr[::2])
print(arr[::-1])
print(arr[2:5])

# 索引
print(arr[0])

print("=" * 100)

array = np.array([[1, 2, 3], [4, 5, 6]])
print(array)
print(array[0, 0])

print(array[:, 1])

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 2, 4, 6, 1, 9])

# 对arr的计算实际上会作用在每一个元素上
print(arr % 2 == 1)

# 布尔值索引
# 取出所有的奇数
print(arr[arr % 2 == 1])
print(arr[arr > 5])

x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(x)
# 带入公式进行计算
y = 2 * x + 100
print(y)

print("=" * 100)

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr)
print(arr.shape)
# 重构
print(arr.reshape((3, 3)))

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[1, 2, 3], [4, 5, 6]])

# 通过 vstack 沿纵轴拼接
print(np.vstack((arr1, arr2)))
# 通过 hstack 沿横轴拼接
print(np.hstack((arr1, arr2)))

print("=" * 100)

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
# 左右翻转
print(np.fliplr(arr1))
# 上下翻转
print(np.flipud(arr1))

print("=" * 100)

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[10, 20, 30], [40, 50, 60]])
# 对位运算
print(arr1 + arr2)
print(arr1 - arr2)
print(arr1 * arr2)
print(arr1 / arr2)
print(arr1 // arr2)
print(arr1 % arr2)

print("=" * 100)
# 矩阵相乘
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = arr1.T
print(arr1)
print(arr2)
print(arr1.dot(arr2))

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 自然对数
print(np.log(arr))

# 差分
arr = np.array([1, 1, 2, 4, 7, 1, 3, 4, 6])

print(np.diff(arr))

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr1)
# axis: 控制差分的方向
print(np.diff(arr1, axis=1))

arr = np.array([1, 1, 2, 4, 7, 1, 3, 4, 6])

# 最大值
print(np.amax(arr))
print(np.amin(arr))
print(np.median(arr))
print(np.mean(arr))
print(np.std(arr))

# 生成标准正太分布的数据
arr = np.random.normal(size=(10, 10))
print(arr)
